home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12049 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.9 KB  |  236 lines

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader Jr)
  3. Newsgroups: comp.lang.c++,gnu.help.g++
  4. Subject: Out of Memory error?
  5. Date: 17 Mar 1996 20:06:17 -0500
  6. Organization: Panix
  7. Message-ID: <4iicu9$kgm@panix.com>
  8. NNTP-Posting-Host: panix.com
  9.  
  10. I must be making progress in my effort to learn to program,
  11. because this is a new error for me!
  12.  
  13. My program uses a class Args which has two dynamically
  14. allocated arrays of strings one for labels and the other for values.
  15.  
  16. An Args object is created by a static function in the Triangle
  17. class and passed to main.  The values are loaded into the
  18. Args object in main and the object is then passed back to a
  19. "make instance of" static function in Triangle. However, when
  20. I try to load the third value, I get:
  21.  
  22. assertion "val[valcnt] != 0" failed: file "args.C", line 49
  23. zsh: abort (core dumped)  ./a.out 
  24.  
  25. A new one on me.  I have run through the program, I know that
  26. it is crude, but I think that it should be working.  The
  27. failed assert indicates to me that I have run out of
  28. memory...how can this be.  What do I do now!?!  Below is the
  29. code...any pointers in how to think about this type of issue/problem 
  30. will be appreciated!
  31.  
  32. Arthur
  33.  
  34. ************
  35. // shape.h
  36. // declaration of abstract base class shape
  37. #ifndef SHAPE_H
  38. #define SHAPE_H
  39.  
  40. class Shape {
  41. public:
  42.         virtual void draw() const = 0; // pure virtual
  43. };
  44.  
  45. #endif
  46.  
  47. // point.h
  48. // Declaration for class Point which extends Shape
  49.  
  50. #ifndef POINT_H
  51. #define POINT_H
  52.  
  53. #include "shape.h"
  54.  
  55. class Point : public Shape {
  56. public:
  57.         Point(float = 0.0, float = 0.0);
  58.         float getx() const;
  59.         float gety() const;
  60.         void setfill(char);
  61.         virtual void draw() const;
  62.  
  63. private:
  64.         float x;
  65.         float y;
  66.         char fill;
  67. };
  68.  
  69. #endif
  70.  
  71. // point.C
  72. // definitions for abstract base class
  73.  
  74. #include <iostream.h> // included for draw function to be
  75.                                           // replaced with
  76. curses routine
  77. #include "point.h"
  78.  
  79. // Constructor
  80. Point::Point(float a, float b)
  81. {
  82.         x = a;
  83.         y = b;
  84.         setfill('*');
  85. }
  86.  
  87. // Get x
  88. float Point::getx() const { return x; }
  89.  
  90. // Get y
  91. float Point::gety() const { return y; }
  92.  
  93. // Set fill
  94. void Point::setfill(char c) { fill = c; }
  95.  
  96. // Draw
  97. void Point::draw() const
  98. {
  99.         cout << "[" << x << ", " << y << "]" << endl;
  100. }
  101.  
  102. // triangle.h
  103. // declaration of class Triangle extends shape
  104.  
  105. #ifndef TRIANGLE_H
  106. #define TRIANGLE_H
  107.  
  108. #include "point.h"
  109. #include "args.h"
  110.  
  111. class Triangle : public Point {
  112. public:
  113.         Triangle(float = 0.0, float = 0.0, float = 0.0);
  114.         float gets() const;
  115.         virtual void draw() const;
  116.  
  117.         static Args &getargs();
  118.         static Triangle *makeinstance(Args &);
  119. private:
  120.         float side;
  121. };
  122.  
  123. #endif
  124.  
  125. // triangle.C
  126. // Class definition
  127.  
  128. #include <iostream.h>
  129. #include <string.h>
  130. #include <assert.h>
  131. #include <stdlib.h>
  132. #include "tri.h"
  133. #include "args.h"
  134.  
  135. // Static Functions
  136.  
  137. // Get Args
  138. Args &Triangle::getargs()
  139. {
  140.         Args &a = *(new Args());
  141.         a.addlbl("X coordinate: ");
  142.         a.addlbl("Y coordinate: ");
  143.         a.addlbl("Length of sides: ");
  144.         a.addlbl("Fill character: ");
  145.  
  146.         return a;
  147. }
  148.  
  149. // Make instance of triangle
  150. Triangle *Triangle::makeinstance(Args &a)
  151. {
  152.         const int MINX = 0;
  153.         const int MINY = 0;
  154.         const int MAXX = 80;
  155.         const int MAXY = 15;
  156.         float tmpx;
  157.         float tmpy;
  158.         float tmps;
  159.         char tmpfill;
  160.  
  161.         // Pop x value
  162.         a.nextval();
  163.         tmpx = atof(a.getval());
  164.         if(tmpx < MINX || tmpx > MAXX){
  165.                 cout << "X out of bounds" << endl;
  166.                 return 0;
  167.         }
  168.  
  169.         // Pop y value
  170.         a.nextval();
  171.         tmpy = atof(a.getval());
  172.         if(tmpy < MINY || tmpy > MAXY){
  173.                 cout << "y out of bounds" << endl;
  174.                 return 0;
  175.         }
  176.  
  177.         // Pop s value
  178.         a.nextval();
  179.         tmps = atof(a.getval());
  180.         if(tmps <= 0){
  181.                 cout << "triangle cannot have side 0" << endl;
  182.                return 0;
  183.         }
  184.  
  185.         Triangle *t = new Triangle(tmpx, tmpy, tmps);
  186.         assert(t != 0);
  187.  
  188.         //Pop fill value
  189.         a.nextval();
  190.         tmpfill = a.getval()[0];
  191.         t->setfill(tmpfill);
  192.  
  193.         return t;
  194. }
  195.  
  196. // Constructor
  197. Triangle::Triangle(float a, float b, float c)
  198.         : Point(a,b)
  199. { side = c > 0 ? c : 0; }
  200.  
  201. // Get side
  202. float Triangle::gets() const { return side; }
  203.  
  204. // Draw
  205. void Triangle::draw() const
  206. {
  207.         cout << "Trinagle: " << endl;
  208.         cout << "Anchor: ";
  209.         Point::draw();
  210.         cout << endl << "Side: " << gets() << endl;
  211. }
  212.  
  213. #include <iostream.h>
  214. #include "tri.h"
  215. #include "args.h"
  216. #include "shape.h"
  217.  
  218. main()
  219. {
  220.         Args &a = Triangle::getargs();
  221.         Shape *s;
  222.         char val[5];
  223.  
  224.         for(int i = a.getlblcnt(); i > 0; i--){
  225.                 cout << a.getlbl();
  226.                 cin >> val;
  227.                 a.addval(val);
  228.                 a.nextlbl();
  229.         }
  230.  
  231.         s = Triangle::makeinstance(a);
  232.         s->draw();
  233.  
  234.         return 0;
  235. }
  236.